package com.eddie.principle.ocp.improve;

/**
 * @author Eddie
 * @date 2022/4/27 17:06
 */
public class Ocp {
    public static void main(String[] args) {
        //使用看看存在的问题
        GraphicEditor graphicEditor = new GraphicEditor();
        graphicEditor.drawShape(new Rectangle());
        graphicEditor.drawShape(new Circle());
        graphicEditor.drawShape(new Triangle());
        graphicEditor.drawShape(new OtherGraphic());
    }
}

/**
 * 用于绘图的类 [使用方 ]
 */
class GraphicEditor {
    /**
     * 根据Type绘制不同的图形
     */
    public void drawShape(Shape s) {
        s.draw();
    }
}

/**
 * 基类
 */
abstract class Shape {
    int mType;

    /**
     * 抽象方法
     */
    public abstract void draw();
}

class Rectangle extends Shape {
    Rectangle() {
        super.mType = 1;
    }

    @Override
    public void draw() {
        System.out.println("绘制矩形");
    }
}

class Circle extends Shape {
    Circle() {
        super.mType = 2;
    }

    @Override
    public void draw() {
        System.out.println("绘制圆形");
    }
}

/**
 * 新增画三角形
 */
class Triangle extends Shape {
    Triangle() {
        super.mType = 3;
    }

    @Override
    public void draw() {
        System.out.println("绘制三角形");
    }
}

/**
 * 新增一个图形
 */

class OtherGraphic extends Shape {

    OtherGraphic() {
        super.mType = 4;
    }

    @Override
    public void draw() {
        System.out.println("绘制其他图形");
    }
}